<template>
    <div class="pagebody">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 运营费用 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="search-box">
                <el-form :inline="true">
                    <el-form-item label="开始时间">
                        <el-date-picker
                            v-model="searchSet.startTime"
                            clearable
                            type="datetime"
                            placeholder="选择日期时间"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            align="right"
                            :picker-options="pickerTimeOptions"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="结束时间">
                        <el-date-picker
                            v-model="searchSet.endTime"
                            clearable
                            type="datetime"
                            placeholder="选择日期时间"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            align="right"
                            :picker-options="pickerTimeOptions"
                        ></el-date-picker>
                    </el-form-item>
                    <el-form-item label="货币种类">
                        <el-select v-model="searchSet.currencyid" placeholder="选择货币种类" clearable>
                            <el-option
                                v-for="(item, index) in currencyList"
                                :key="index"
                                :label="`${item.name}(${item.code})`"
                                :value="item.id"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="经手人">
                        <el-input
                            v-model="searchSet.name"
                            placeholder="请输入经手人姓名"
                            maxlength="10"
                            show-word-limit
                            clearable
                            class="formInput"
                        ></el-input>
                    </el-form-item>
                    <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="handleAddNew">添加记录</el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="handleImport">导入</el-button>
                    <el-button type="primary" icon="el-icon-download" @click="handleExport">导出</el-button>
                </el-form>
            </div>

            <el-card class="SumCard" :body-style="{ padding: '10px 20px' }">
                <div class="FlexRowVCenter">
                    <div style="font-weight: bold">合计：</div>
                    <div v-for="(item, index) in sumList" :key="index" class="SumItem FlexRowVCenter">
                        <i class="el-icon-money SumItemIcon"></i>
                        <div>
                            <div class="SumItemLabel">{{ item.currencyname }}</div>
                            <div>{{ item.totalAmount }}</div>
                        </div>
                    </div>
                </div>
            </el-card>
            <el-table :data="tableData" v-loading="tableLoading" border class="table" ref="mainTable">
                <!-- <el-table-column prop="id" label="ID" align="center"></el-table-column> -->
                <el-table-column prop="name" label="费用项目" align="center"></el-table-column>
                <el-table-column prop="currencyname" label="货币名称" align="center"></el-table-column>
                <el-table-column prop="amount" label="货币金额" align="center"></el-table-column>
                <el-table-column prop="operator" label="经手人" align="center"></el-table-column>
                <el-table-column prop="paytime" label="发生时间" align="center"></el-table-column>
                <el-table-column prop="addtime" label="添加时间" align="center"></el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="sizes, total, prev, pager, next"
                    :current-page="query.index"
                    :page-sizes="[10, 20, 30, 40, 50, 100]"
                    :page-size="query.pageSize"
                    :total="itemTotal"
                    @current-change="handlePageChange"
                    @size-change="handlePageSizeChange"
                ></el-pagination>
            </div>
        </div>

        <el-dialog title="添加操作" v-dialogDrag center :visible.sync="editVisible" width="60%">
            <el-form ref="form" size="mini" :model="form" :rules="formRules" label-width="150px">
                <el-form-item label="名称" prop="name">
                    <el-input
                        v-model="form.name"
                        placeholder="请输入名称"
                        maxlength="20"
                        show-word-limit
                        clearable
                        class="formInput"
                    ></el-input>
                </el-form-item>
                <el-form-item label="货币种类" prop="currencyid">
                    <el-select v-model="form.currencyid" class="formInput" placeholder="选择货币种类" clearable>
                        <el-option
                            v-for="(item, index) in currencyList"
                            :key="index"
                            :label="`${item.name}(${item.code})`"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="货币金额" prop="amount">
                    <el-input
                        v-model="form.amount"
                        placeholder="请输入货币金额"
                        maxlength="10"
                        show-word-limit
                        clearable
                        class="formInput"
                    ></el-input>
                </el-form-item>
                <el-form-item label="发生时间">
                    <el-date-picker
                        v-model="form.paytime"
                        clearable
                        type="datetime"
                        placeholder="选择日期时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        align="right"
                        :picker-options="pickerTimeOptions"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="经手人" prop="operator">
                    <el-input
                        v-model="form.operator"
                        placeholder="请输入经手人"
                        maxlength="20"
                        show-word-limit
                        clearable
                        class="formInput"
                    ></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveNew">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog title="运营费用导入" v-dialogDrag center :visible.sync="importVisible" width="80%">
            <el-form ref="importform" :model="importform" label-width="120px">
                <el-form-item label="运营费用表">
                    <div class="FlexRow">
                        <div>
                            <el-button type="primary" icon="el-icon-download" style="margin: 5px 10px 0 0" @click="downloadXlSX"
                                >下载运营费用模板</el-button
                            >
                        </div>
                        <runFeeFileUpload
                            style="margin: 5px 10px"
                            accept="xlsx/*"
                            ref="UploadImage"
                            :limit="1"
                            id="importformupload"
                            tip="只能上传xlsx文件"
                            v-on:update:excelData="UpdateExcel"
                        >
                        </runFeeFileUpload>
                    </div>

                    <el-table :data="ImportTableData" stripe border max-height="300" style="width: 100%">
                        <el-table-column v-for="(item, index) in ImportTableHeader" :key="index" :prop="String(index)" :label="item">
                        </el-table-column>
                    </el-table>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="importVisible = false">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import API from '../../../api/index'; //读入restfulAPI
import StringTool from '../../../utils/StringTool';
import dataTable from './dataTable';
import runFeeFileUpload from '../../common/upload/runFeeFileUpload.vue';
export default {
    name: 'runFee',
    mixins: [dataTable],
    components: {
        runFeeFileUpload
    },
    data() {
        let CheckDigit = (rule, value, callback) => {
            if (!StringTool.DigitNumber(String(value))) {
                return callback(new Error('只能为整数和保留两位小数'));
            }
            if (Number(value) < 0) {
                return callback(new Error('只能为正数'));
            }
            callback();
        };
        return {
            searchSet: {
                startTime: '',
                endTime: '',
                currencyid: '',
                name: ''
            },
            currencyList: [],
            sumList: [],
            editVisible: false,
            form: {},
            formRules: {
                name: [{ required: true, message: '参数不能为空', trigger: 'blur' }],
                currencyid: [{ required: true, message: '参数不能为空', trigger: 'blur' }],
                amount: [
                    { required: true, message: '参数不能为空', trigger: 'blur' },
                    { validator: CheckDigit, required: true, trigger: 'blur' }
                ]
            },
            importVisible: false,
            importform: {},
            ImportTableHeader: [],
            ImportTableData: []
        };
    },
    created() {
        this.init();
    },
    mounted() {},
    computed: {},
    methods: {
        //页面初始化
        init() {
            this.getData();
            API.StandardDoWith(API.Currency.Get({ status: 1 }), this, {
                success: (detail) => {
                    this.currencyList = detail;
                }
            });
        },
        getData() {
            this.tableLoading = true;
            API.StandardDoWith(API.RunFee.Get(this.getQuery()), this, {
                success: (detail) => {
                    this.itemTotal = detail.count;
                    this.tableData = detail.list;
                    this.sumList = detail.sumList;
                },
                complete: () => {
                    this.tableLoading = false;
                }
            });
        },

        handleAddNew() {
            this.form = {
                agentid: null,
                currencyid: null,
                amount: ''
            };
            this.editVisible = true;
        },
        handleImport() {
            this.importVisible = true;
            this.importform.data = [];
            this.ImportTableHeader = [];
            this.ImportTableData = [];
        },

        saveNew() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.form.amount = Number(this.form.amount);
                    API.StandardDoWith(API.RunFee.Post(this.form), this, {
                        success: (detail) => {
                            this.editVisible = false;
                            this.$message.success(`成功新建记录`);
                            this.getData();
                        }
                    });
                } else {
                    this.$message.error(`表单验证失败，无法提交`);
                    return false;
                }
            });
        },
        downloadXlSX() {
            window.location.href =  this.$store.state.baseURL + 'api/Runfeefile';
        },
        UpdateExcel(excelData) {
            this.importform.data = excelData;
            this.ImportTableHeader = [];
            if (this.importform && this.importform.data && this.importform.data.length > 0) {
                this.ImportTableHeader = this.importform.data[0];
            }
            this.ImportTableData = [];
            if (this.importform && this.importform.data && this.importform.data.length > 1) {
                let list = [];
                this.importform.data.map((it) => {
                    list.push(it);
                });
                list.splice(0, 1);
                this.ImportTableData = list;
            }
        },
        handleExport() {
            this.$confirm('是否导出Excel数据?', '提示')
                .then(() => {
                    API.StandardDownFile(API.RunFeeExport(), '运营费用数据.xlsx');
                })
                .catch((err) => {});
        }
    }
};
</script>

<style scoped>
@import '../../../assets/css/dataTable.css';
</style>
